package gang.com.twoshopping.weight;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.widget.TintTypedArray;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import gang.com.twoshopping.R;

/**
 * Created by Administrator on 2017/5/7.
 */

public class CnToolBar extends Toolbar {

    private LayoutInflater mInflater;
    private View mChildView;
    private TextView mTexttile;
    private EditText mEdittext;
    private ImageButton mRightImagebutton;

    public CnToolBar(Context context) {
        super(context);
    }

    public CnToolBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CnToolBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        if (attrs != null) {
            final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
                    R.styleable.CnToolbar, defStyleAttr, 0);
            final Drawable rightIcon = a.getDrawable(R.styleable.CnToolbar_rightButtonIcon);
            if (rightIcon != null) {
                //这是原生的方法我们需要修改一下
                //setNavigationIcon(navIcon);
                setRightButtonIcon(rightIcon);

            }
            //注意不要忘记回收
            a.recycle();
        }
    }

    private void setRightButtonIcon(Drawable icon) {
        if (mRightImagebutton != null) {
            mRightImagebutton.setImageDrawable(icon);
            mRightImagebutton.setVisibility(VISIBLE);
        }
    }

    private void initView() {
        if (mChildView == null) {
            mInflater=LayoutInflater.from(getContext());
            mChildView = mInflater.inflate( R.layout.toolbar_init, null);

            mTexttile = (TextView) mChildView.findViewById(R.id.toolbar_title);
            mEdittext = (EditText) mChildView.findViewById(R.id.toolbar_searchview);
            mRightImagebutton = (ImageButton) mChildView.findViewById(R.id.toolbar_rightButton);
            LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
            //添加自定义的布局到Toolbar
            addView(mChildView, lp);
        }
    }
    //为了避免冲突，ToolBar已经有了一个setTitle方法。我们只能重写

    @Override
    public void setTitle(@StringRes int resId) {
        setTitle(getContext().getText(resId));
    }

    //先调用父类的方法
    @Override
    public void setTitle(CharSequence title) {
        initView();
        if (mTexttile != null) {
            mTexttile.setText(title);
            showTitle();
        }
    }

    /**
     * 设置标题或者搜索框是否显示
     */
    private void showTitle() {
        mTexttile.setVisibility(View.VISIBLE);
    }

    private void hideTitle() {
        mTexttile.setVisibility(View.GONE);
    }

    private void showSearchview() {
        mEdittext.setVisibility(View.VISIBLE);
    }

    private void hideSearchview() {
        mEdittext.setVisibility(View.GONE);
    }

}
